import base64
import datetime
from typing import List
from pydantic import Field, BaseModel, root_validator
from package.fastapi.schema import Page, BatchId


class EditPanelib(BaseModel):
    name: str = Field(None, title='名称')
    description: str = Field(None, title='描述')
    type: str = Field(None, title='类型')

    search: dict = Field(None, title='搜索')
    configure: dict = Field(None, title='图表配置 背景/风格/xxx')
    echart: dict = Field(None, title='图表选项')
    cron: dict = Field(None, title='定时任务')


class CreatePanelib(EditPanelib):
    pass 


class PanelibOut(CreatePanelib):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PagePanelibs(Page):
    """获取页码条目"""
    data: List[PanelibOut] = Field(..., title='')


class _DashboardConfigure(BaseModel):
    background: str = Field(..., title='背景图')
    style: str = Field(..., title='风格')
    interval: int = Field(60, title='刷新间隔')


class _DashboardLayOut(BaseModel):
    category: str = Field(..., title='图表类型 ')
    panel_id: str = Field(..., title='图片ID')
    x: int = Field(..., title='x')
    y: int = Field(..., title='y')
    w: int = Field(..., title='width')
    h: int = Field(..., title='height')


class EditDashboard(BaseModel):
    name: str = Field(None, title='名称')
    description: str = Field(None, title='描述')
    configure: _DashboardConfigure = Field(None, title='仪表板配置')
    layout: List[_DashboardLayOut] = Field(None, title='布局')

class CreateDashboard(EditDashboard):
    pass 


class DashboardOut(CreateDashboard):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class DashboardItemOut(CreateDashboard):
    id: str = Field(..., title='ID')
    panels: dict = Field(..., title='图表')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageDashboards(Page):
    """获取页码条目"""
    data: List[DashboardOut] = Field(..., title='')


# 图表搜索条件
class PanelibSearch(BaseModel):
    search: dict = Field(None, title='搜索')
    configure: dict = Field(None, title='图表配置 背景/风格/xxx')


class EditPanelibCache(BaseModel):
    cache: dict = Field(..., title='')


class PanelibCacheOut(EditPanelibCache):
    cache: dict = Field(..., title='')
    id: str = Field(..., title='ID')
    updated_at: datetime.datetime = Field(None, title='')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class DashboardItemOut(DashboardOut):
    panels: List[PanelibOut] = Field(..., title='xx')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}